/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import model.dao.DaoBase;
import model.dao.DisciplinaJpaController;
import model.dao.exceptions.NonexistentEntityException;
import model.pojo.Disciplina;

/**
 *
 * @author leonidia
 */
public class EditarDisciplina extends javax.swing.JFrame {

    /**
     * Creates new form EditarDisciplina
     */
    DaoBase daoBase;
    Disciplina disciplina;
    List<Disciplina> listaDisciplina;

    public EditarDisciplina(DaoBase daoBase) {
        initComponents();
        this.daoBase = daoBase;
        atualizarCampos();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        Ementa = new javax.swing.JTextArea();
        Nome = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        CargaHoraria = new javax.swing.JTextField();
        Editar = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        TabelaDisciplina = new javax.swing.JTable();
        jLabel5 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Editar Disciplina");

        Ementa.setColumns(20);
        Ementa.setRows(5);
        jScrollPane1.setViewportView(Ementa);

        jLabel4.setText("Carga horaria");

        jLabel2.setText("Nome");

        jLabel3.setText("Ementa");

        CargaHoraria.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CargaHorariaActionPerformed(evt);
            }
        });

        Editar.setText("Editar");
        Editar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                EditarActionPerformed(evt);
            }
        });

        TabelaDisciplina.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Nome", "Ementa", "Carga Horaria"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        TabelaDisciplina.setToolTipText("");
        TabelaDisciplina.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                TabelaDisciplinaMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(TabelaDisciplina);

        jLabel5.setText("Lista de Disciplinas");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(168, 168, 168)
                                .addComponent(jLabel1))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(35, 35, 35)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel2)
                                    .addComponent(CargaHoraria, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(Nome, javax.swing.GroupLayout.PREFERRED_SIZE, 182, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel3)
                                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 266, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(161, 161, 161)
                                .addComponent(Editar)))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 15, Short.MAX_VALUE)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(153, 153, 153)
                .addComponent(jLabel5)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(Nome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel4)
                .addGap(26, 26, 26)
                .addComponent(CargaHoraria, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(Editar))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void CargaHorariaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CargaHorariaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_CargaHorariaActionPerformed

    private void TabelaDisciplinaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_TabelaDisciplinaMouseClicked
        // TODO add your handling code here:
        disciplina = listaDisciplina.get(TabelaDisciplina.getSelectedRow());
        Nome.setText(disciplina.getNome());
        CargaHoraria.setText(Integer.toString(disciplina.getCargaHoraria()));
        Ementa.setText(disciplina.getEmenta());
    }//GEN-LAST:event_TabelaDisciplinaMouseClicked

    private void EditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_EditarActionPerformed
        // TODO add your handling code here:
        disciplina.setNome(Nome.getText());
        try {  
            Integer cargaHoraria = Integer.parseInt(CargaHoraria.getText());  
        } catch (Exception z) {   
            JOptionPane.showMessageDialog(this, "A carga horária deve conter apenas um número", "Erro!", JOptionPane.ERROR_MESSAGE);  
            CargaHoraria.setText("");  
            return;  
        } 
        disciplina.setEmenta(Ementa.getText());
        DisciplinaJpaController disciplinaJpaController = new DisciplinaJpaController(null,daoBase.returnFactory());
        try {
            disciplinaJpaController.edit(disciplina);
            JOptionPane.showMessageDialog(null, "Alteração feita com sucesso");
        } catch (NonexistentEntityException ex) {
            Logger.getLogger(EditarDisciplina.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(EditarDisciplina.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_EditarActionPerformed

    /**
     * @param args the command line arguments
     */
    public void run() {
        setVisible(true);
    }

    public void stop() {
        setVisible(false);
    }

    public void atualizarCampos() {
        DefaultTableModel model = (DefaultTableModel) TabelaDisciplina.getModel();
        DisciplinaJpaController disciplinaJpaController = new DisciplinaJpaController(null, daoBase.returnFactory());
        listaDisciplina = disciplinaJpaController.findDisciplinaEntities();
        for (Disciplina item : listaDisciplina) {
            model.addRow(new Object[]{item.getNome(), item.getEmenta(), item.getCargaHoraria()});
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField CargaHoraria;
    private javax.swing.JButton Editar;
    private javax.swing.JTextArea Ementa;
    private javax.swing.JTextField Nome;
    private javax.swing.JTable TabelaDisciplina;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    // End of variables declaration//GEN-END:variables
}
